class System
  def self.default_blog
    default_blog_id = global_setting('default_blog')
    
    if default_blog_id
      Blog.find(default_blog_id)
    else
      Blog.find(:all).first
    end
  end
  
  # Convenience method for retrieving a setting for a given blog.
  # The blog argument can be a numeric blog ID, a blog name, or
  # a Blog object.
  def self.blog_setting(blog, name)
    if blog.kind_of? Integer or blog =~ /^\d+$/
      conditions = ["blog_id = ? AND name = ?", blog, name]
    elsif blog.kind_of? Blog
      conditions = ["blog_id = ? AND name = ?", blog.id, name]
    elsif blog.kind_of? String
      conditions = ["blog_id = ? AND name = ?", Blog.find_by_name(blog).id, name]
    else
      raise "'#{blog}' is not a valid Blog identifier."
    end
    
    Setting.find(:first, :conditions => conditions)
  end
  
  def self.global_setting(name)
    Setting.find(:first, :conditions => ["blog_id IS NULL AND name = ?", name])
  end
end